<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>5-动态路由匹配2</title>
    <!-- 导入 vue 文件 -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  </head>
  <body>
    <div id="app">
      <router-link to="/user1/1">User1</router-link>
      <router-link to="/user2/2">User2</router-link>
      <router-view></router-view>
    </div>
    <script>
      var User1 = {
        // 2：直接在props: ['id']，把路由传过来的id1给拿取到， 直接在template模板中使用即可
        props: ['id'],
        template: `
        <div>
          <h1>User1 - 组件, 路由id是： {{id}}</h1>
          <router-link to="/user1/tab1">User1/Tab1组件</router-link>
          <router-link to="/user1/tab2">User1/Tab2组件</router-link>
          <router-view></router-view>
        </div>
        `,
      };
      var User2 = {
        props: ['id'],
        template: `<h1>User2 - 组件, 路由id是： {{id}}</h1>`,
      };
      const Tab1 = {
        template: `<h3>Tab1 - 子路由1</h3>`,
      };
      const Tab2 = {
        template: `<h3>Tab2 - 子路由2</h3>`,
      };
      // 1：props: true直接将id传给子组件， 就不用通过$route.params.id拿取id了，
      const router = new VueRouter({
        routes: [
          {
            path: '/',
            redirect: '/user1',
          },
          {
            path: '/user1/:id',
            component: User1,
            props: true,
            children: [
              {
                path: '/user1/tab1',
                component: Tab1,
              },
              {
                path: '/user1/tab2',
                component: Tab2,
              },
            ],
          },
          {
            path: '/user2/:id',
            component: User2,
            props: true,
          },
        ],
      });

      const vm = new Vue({
        el: '#app',
        data: {},
        router,
      });
    </script>
  </body>
</html>
